#ifndef BITSTREAMNALUMODEL_H
#define BITSTREAMNALUMODEL_H

#include "h264bitstream/h264_stream.h"

#include <QObject>
#include <QAbstractTableModel>
#include <cstdio>

struct naluInfo {
    int seq;
    int ref_idc;
    int type;
    int len;
    QString info;
};

class BitStreamNALUModel : public QAbstractTableModel
{
public:
    BitStreamNALUModel(QObject *parent);
    ~BitStreamNALUModel();
    int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE ;
    int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const Q_DECL_OVERRIDE;
    bool insertRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
    bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) Q_DECL_OVERRIDE;
    struct naluInfo *naluByIndex(int row);
    QModelIndex get_index(int row, int c);
private:
    static void *_thread_parser_file(void *param);
    static void _thread_cleanup(void *param);
public slots:
    void parse_file(QString fname);
signals:
    void resize_rows();
    void resize_columns();
private:
    QString filename;
    /*
     * QVector is not thread safe
     * acquire parse_mutex when access it
     */
    QVector<struct naluInfo*> nalus;
    pthread_mutex_t parse_mutex;

    int row_count;
    char *nal_info;

    int column_count = 4;

    h264_stream_t *h264_stream;
    FILE *infile;
    FILE *h264_dbg_file;
    size_t nal_info_size;

    pthread_t parse_tid;
};

#endif // BITSTREAMNALUMODEL_H
